/**
 * http://ncfdc.nc.gov.cn/default.aspx?id=431&name=gggs_htba
 * 南昌房地产信息网爬虫
 * sunny 2017-06-14
 */

import { Base, Item } from "../base";

// 工具类
const utils = require('../../utils');

//南昌房地产信息网
const ncfdc_url = "http://ncfdc.nc.gov.cn/default.aspx?id=431&name=gggs_htba";

class ncfdc_spider extends Base {
    form: any;
    firstPage: string;

    constructor() {
        super();
        this.name = "南昌房地产信息网";
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = true;
        this.timeout = 1000000;
        this.collection = "fdcxx";
    }

    getPageCount(): number {
        this.form = {};
        var content = this.spider.getSync(ncfdc_url);
        this.firstPage = content;
        if (/AspNetPager1_input',(\d+),'页索引超出范围/.test(content)) {
            var pgCount = parseInt(RegExp.$1);
            console.log("Page Count: " + pgCount);
            return pgCount;
        }
        return 1;
    }

    getItems(pageIndex: number): Item[] {
        var content: string;
        if (pageIndex == 0) {
            content = this.firstPage;
            var $ = this.cheerio.load(content);
            return this.extractItems($, pageIndex, content);
        } else {
            this.form["__EVENTARGUMENT"] = pageIndex;
            this.form["ctl14$AspNetPager1_input"] = pageIndex - 1;
            this.form["ctl12$txtSearch"] = "搜索信息";
            this.form["ctl14$txtCode"] = "";
            this.form["ctl14$txtBegin"] = "";
            this.form["ctl14$txtEnd"] = "";
            this.form["__VIEWSTATEENCRYPTED"] = "";
            content = this.spider.postSync(ncfdc_url, this.form);
            var $ = this.cheerio.load(content);
            return this.extractItems($, pageIndex, content);
        }
    }

    private extractItems($: CheerioSelector, pageIndex: number, data: string): Item[] { // 获取页面中的招标项目信息，$是一个类似jQuery的对象(cheerio)，可以直接按照jQuery方式使用
        var items: Item[] = [];
        var self = this;
        if (pageIndex == 1) {
            this.form = {};
            $("form input[type='hidden']").each(function (index, ele) {
                var e = $(ele);
                var v = e.attr("value");
                var n = e.attr("name");
                if (v) {
                    // console.log(e.attr("name") + " => " + e.attr("value"));
                    self.form[n] = v;
                }
            });
            this.form["__EVENTTARGET"] = "ctl14$AspNetPager1";
            this.inner_process($, items);
        } else {
            this.inner_process($, items);
        }
        return items;
    }

    private inner_process($: CheerioSelector, items: Item[]): void {
        var self = this;
        $(".gggs_zbsTABLE").each(function (index, ele) {
            var e = $(ele);
            var item: Item;
            item = self.buildItem();
            var a = e.find("th a");
            item.title = a.text().trim();
            var tds = e.find("td span");
            (item as any)["项目名称"] = $(tds[0]).text().trim();
            (item as any)["备案时间"] = $(tds[2]).text().trim();
            items.push(item);
        });
    }
}

export = new ncfdc_spider();